﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameJamSnake
{
    class Scorekeeper
    {
        private Scorekeeper()
        {
            m_font = null;
            m_score = 0;
        }

        private int m_score;

        private float elapsedTime = 0;
        private float m_totalSeconds;
        private int m_timeLimit = 300;
        private int m_totalSecondsLeft;
        private int m_minutesLeft;
        private int m_secondsLeft;
        private const int TIME_INCREASE = 60;

        private float elapsedPowerTime = 0;
        private const int POWER_TIME = 20;
        private int m_powerTimeLeft;

        private static Scorekeeper m_instance;
        private SpriteFont m_font;

        public static Scorekeeper Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new Scorekeeper();
                }

                return m_instance;
            }
        }

        public void AddScore(int add)
        {
            m_score += add;
        }

        public int GetScore()
        {
            return m_score;
        }

        public void UpdateTime(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
            m_totalSeconds = elapsedTime / 1000.0f;
            m_totalSecondsLeft = m_timeLimit - (int)m_totalSeconds;
            m_minutesLeft = m_totalSecondsLeft / 60;
            m_secondsLeft = m_totalSecondsLeft % 60;
        }

        public void IncreaseTime()
        {
            m_timeLimit += TIME_INCREASE;
        }
        public void resetTime()
        {
            m_timeLimit = 300;
        }
        public void resetScore()
        {
            m_score = 0;
        }
        public void CountPower(GameTime gameTime)
        {
            elapsedPowerTime += gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
            m_powerTimeLeft = POWER_TIME - (int)elapsedPowerTime;
        }

        public bool PowerOver(bool confirm)
        {
            if (m_powerTimeLeft < 1 || confirm)
            {
                elapsedPowerTime = 0;
                m_powerTimeLeft = 0;
                return true;
            }

            return false;
        }

        public bool GameOver()
        {
            if (m_totalSecondsLeft < 1)
                return true;

            return false;
        }
        
        public void Draw(SpriteBatch sBatch)
        {
           // sBatch.Begin();
            string output = "Score: " + m_score.ToString();
            Vector2 stringSize = m_font.MeasureString(output);
            Vector2 origin = new Vector2(800, 0);
            sBatch.DrawString(m_font, output, origin, Color.DarkMagenta);

            string mprefix = "";
            string sprefix = "";

            if (m_minutesLeft < 10) mprefix = "0"; else mprefix = "";
            if (m_secondsLeft < 10) sprefix = "0"; else sprefix = "";

            string time = "Time: " + mprefix + m_minutesLeft.ToString() + ":" + sprefix + m_secondsLeft.ToString();
            sBatch.DrawString(m_font, time, new Vector2(200, 0), Color.DarkMagenta);

            string powerTime = m_powerTimeLeft.ToString();
            if (!PowerOver(false))
                sBatch.DrawString(m_font, powerTime, new Vector2(600, 0), Color.DarkMagenta);
        }

        public void Load(ContentManager content)
        {
            m_font = content.Load<SpriteFont>("Scorefont");
        }
    }
}
